<template>
  <div class="app-container">
    <!-- 查询区域 -->
    <el-form :model="queryParams" :ref="queryFormRef" inline v-show="showSearch" label-width="auto" @keyup.enter.native="handleQuery">
        <el-form-item label="关键字" prop="keywords" >
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable />
        </el-form-item>
        <el-form-item label="商品分类" prop="categoryId">
          <treeselect v-model="queryParams.categoryId" :options="treeData" :show-count="true" placeholder="" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="商品分组" prop="tagId">
          <lam-dict-select v-model="queryParams.tagId" dictCode="prodTag" dictType="tableDict" placeholder="" clearable />
        </el-form-item>
        <el-form-item label="上架时间" prop="putawayTimeBegin">
          <el-date-picker clearable v-model="queryParams.putawayTimeBegin" value-format="yyyy-MM-dd" type="date" placeholder="上架时间" style="width: 140px;">
          </el-date-picker> -
        </el-form-item>
        <el-form-item prop="putawayTimeEnd">
          <el-date-picker clearable v-model="queryParams.putawayTimeEnd" value-format="yyyy-MM-dd" type="date" placehodelr="上架时间" style="width: 140px;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="tool-bar-btn">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPerms="['mall:product:add']">新增</el-button>
      <el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPerms="['mall:product:edit']">修改</el-button>
      <el-button type="warning" icon="el-icon-finished" :disabled="multiple" @click="handlePutUp" v-hasPerms="['mall:product:putOn']">批量上架</el-button>
      <el-button type="info" icon="el-icon-turn-off" :disabled="multiple" @click="handlePutOff" v-hasPerms="['mall:product:putOff']">批量下架</el-button>
      <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPerms="['mall:product:remove']">删除</el-button>
      <div class="product-status-rd">
        <lam-dict-select type="radioButton" v-model="queryParams.status" dictCode="prod_status_sell_all" placeholder="状态" @change="handleQuery" />
      </div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <!-- 表格区域 -->
    <selected-info :count="selectedRows.length" :onClearSelected="onClearSelected" />
    <el-table :ref="dataTableRef" v-loading="loading" :data="dataList" stripe border @selection-change="onSelectChange"
          :default-sort = "defaultSort" @sort-change="onSortChange" :row-key="rowKey">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="编号" align="center" prop="id" sortable="custom" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="商品主图" align="center" prop="pic" width="100" >
        <template slot-scope="{row}"><img :src="resourceUrl + row.pic " width="80"/></template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="prodName" :show-overflow-tooltip="true" width="250" />
      <el-table-column label="商品原价" align="center" prop="oriPrice"  width="150" />
      <el-table-column label="商品现价" align="center" prop="price"  width="150" />
      <el-table-column label="简要卖点" align="center" prop="brief" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="状态" align="center" prop="statusDictText" width="120">
        <template slot-scope="{row}"><LamStatus :value="row.status" :text="row.statusDictText"/></template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="categoryId"  width="150" />
      <el-table-column label="销量" align="center" prop="soldNum" width="150"/>
      <el-table-column label="商品库存" align="center" prop="totalStocks"  width="150" />
      <el-table-column label="配送方式" align="center" prop="deliveryModeDictText"  width="150" />
      <el-table-column label="运费模板" align="center" prop="deliveryTemplateIdDictText"  width="150" />
      <el-table-column label="上架时间" align="center" prop="putawayTime" width="180"/>
      <el-table-column label="录入时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="200">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)" v-hasPerms="['mall:product:edit']">修改</el-button>
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPerms="['mall:product:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="!loading" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"  @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <EditProduct ref="formDialog" @ok="getList" />

  </div>
</template>

<script>
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {httpGet, httpDel} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import EditProduct from './edit';

export default {
  name: "Product",
  mixins:[IndexListMixin],
  components: {
    EditProduct,
    Treeselect
  },
  data() {
    return {
      treeData:[],
      // 查询参数
      queryParams: {
        keywords: null,
        prodNameKey: null,
        shopId: null,
        brief: null,
        status: -1,
        categoryId: null,
        tagId: null,
        putawayTimeBegin: null,
        putawayTimeEnd: null,
        pageNum: 1,
        pageSize: 10
      },
      url: {
        base: '/mall/product/',
      	list: '/mall/product/list',
        putOn: '/mall/product/putOn',
        putOff: '/mall/product/putOff',
        listCategoryTree: '/mall/category/treeselect'
      }
    };
  },
  created() {
    this.getList();
    this.loadTreeData()
  },
  computed:{
    resourceUrl() {
      return this.$store.getters.resourceUrl
    }
  },
  methods: {
    getList() {
      this.loadData(this.url.list);
    },

    /** 查询下拉树列表 */
    loadTreeData(){
      this.$httpGet(this.url.listCategoryTree).then(res => {
        this.treeData = res.data;
      });
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.formDialog.toAdd("添加商品");
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.selectedRowKeys
      this.$refs.formDialog.toUpdate("修改商品", httpGet(this.url.base + id));
    },

    /** 上架按钮操作 */
    handlePutUp(row) {
      if(this.selectedRows.find(t=>t.status==1)){
        this.msgError("选择中有已上架商品，请选择已下架商品！");
        return;
      }
      const ids = row.id || this.selectedRowKeys
      this.$confirm("确认要上架这【"+ids.length+"】个商品?", "确认", {type: "warning"}).then(() => {
        this.$httpPost(this.url.putOn, {id: ids.join()}).then(res=>{
          this.msgSuccess("商品上架成功！");
          this.getList()
        })
      });
    },

    /** 下架按钮操作 */
    handlePutOff(row) {
      if(this.selectedRows.find(t=>t.status==0)){
        this.msgError("选择中有已下架商品，请选择已上架商品！");
        return;
      }
      const ids = row.id || this.selectedRowKeys
      this.$confirm("确认要下架这【"+ids.length+"】个商品?", "确认", {type: "warning"}).then(() => {
        this.$httpPost(this.url.putOff, {id: ids.join()}).then(res=>{
          this.msgSuccess("商品下架成功！");
          this.getList()
        })
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selectedRowKeys
      this.deleteData(this.url.base, ids);
    },

  }
};
</script>
<style scoped>
  .product-status-rd{
    display: inline-block;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
